#ifndef GUDB_LOGGER_H
#define GUDB_LOGGER_H

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <time.h>
#include <pthread.h>

#ifdef __cplusplus
extern "C" {
#endif

// Log levels (matches product spec requirements)
typedef enum {
    LOG_LEVEL_DEBUG = 0,
    LOG_LEVEL_INFO = 1,
    LOG_LEVEL_WARN = 2,
    LOG_LEVEL_ERROR = 3
} log_level_t;

// Logger configuration
typedef struct {
    char log_file_path[256];        // Path to gudb.log
    log_level_t min_level;          // Minimum log level to write
    uint64_t max_file_size;         // Max file size before rotation (100MB default)
    uint32_t max_rotated_files;     // Number of rotated files to keep
    bool enable_console_output;     // Also log to console
    bool enable_timestamps;         // Include timestamps in log entries
    bool enable_thread_id;          // Include thread ID in log entries
} logger_config_t;

// Logger instance
typedef struct {
    FILE* log_file;                 // Current log file handle
    logger_config_t config;         // Logger configuration
    pthread_mutex_t mutex;          // Thread safety
    uint64_t current_file_size;     // Current log file size
    uint64_t total_entries;         // Total log entries written
    time_t start_time;              // Logger start time
    bool initialized;               // Initialization status
} logger_t;

// Global logger instance
extern logger_t* g_logger;

// Logger lifecycle functions
logger_t* logger_create(const logger_config_t* config);
int logger_init(logger_t* logger);
void logger_destroy(logger_t* logger);
int logger_set_global(logger_t* logger);
logger_t* logger_get_global(void);

// Logging functions
void logger_log(logger_t* logger, log_level_t level, const char* component, 
                const char* format, ...);
void logger_log_query(logger_t* logger, const char* query, uint64_t execution_time_ms,
                      uint64_t rows_returned, const char* error_msg);
void logger_log_slow_query(logger_t* logger, const char* query, 
                          uint64_t execution_time_ms, uint64_t rows_returned);
void logger_log_auth_event(logger_t* logger, const char* username, 
                          const char* event, bool success);
void logger_log_performance(logger_t* logger, const char* metric_name, 
                           double value, const char* unit);

// Log level utilities
const char* log_level_to_string(log_level_t level);
log_level_t log_level_from_string(const char* level_str);

// File rotation
int logger_rotate_if_needed(logger_t* logger);
int logger_force_rotate(logger_t* logger);

// Log statistics
typedef struct {
    uint64_t total_entries;
    uint64_t entries_by_level[4];   // Count per log level
    uint64_t current_file_size;
    uint64_t total_rotations;
    time_t last_rotation_time;
} logger_stats_t;

int logger_get_stats(logger_t* logger, logger_stats_t* stats);

// Convenience macros for global logger
#define LOG_DEBUG(component, format, ...) \
    do { if (g_logger) logger_log(g_logger, LOG_LEVEL_DEBUG, component, format, ##__VA_ARGS__); } while(0)

#define LOG_INFO(component, format, ...) \
    do { if (g_logger) logger_log(g_logger, LOG_LEVEL_INFO, component, format, ##__VA_ARGS__); } while(0)

#define LOG_WARN(component, format, ...) \
    do { if (g_logger) logger_log(g_logger, LOG_LEVEL_WARN, component, format, ##__VA_ARGS__); } while(0)

#define LOG_ERROR(component, format, ...) \
    do { if (g_logger) logger_log(g_logger, LOG_LEVEL_ERROR, component, format, ##__VA_ARGS__); } while(0)

// Query-specific logging macros
#define LOG_QUERY(query, exec_time, rows, error) \
    do { if (g_logger) logger_log_query(g_logger, query, exec_time, rows, error); } while(0)

#define LOG_SLOW_QUERY(query, exec_time, rows) \
    do { if (g_logger) logger_log_slow_query(g_logger, query, exec_time, rows); } while(0)

#define LOG_AUTH_EVENT(username, event, success) \
    do { if (g_logger) logger_log_auth_event(g_logger, username, event, success); } while(0)

#define LOG_PERFORMANCE(metric, value, unit) \
    do { if (g_logger) logger_log_performance(g_logger, metric, value, unit); } while(0)

// Default configuration
static inline logger_config_t logger_default_config(void) {
    logger_config_t config = {
        .log_file_path = "gudb.log",
        .min_level = LOG_LEVEL_INFO,
        .max_file_size = 100 * 1024 * 1024,  // 100MB as per spec
        .max_rotated_files = 5,
        .enable_console_output = false,
        .enable_timestamps = true,
        .enable_thread_id = true
    };
    return config;
}

#ifdef __cplusplus
}
#endif

#endif // GUDB_LOGGER_H